<?php
/*'****************************************************************************\
    CHTML 0.2.1
 	Copyright (C) 2007, 2008 Alexey Ilyin

 	This library is free software; you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published
	by the Free Software Foundation; either version 2.1 of the License, or
	(at your option) any later version.

 	This library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

 	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

\******************************************************************************/

require_once 'cmarkup.inc.php';

class CHTMLDocument extends CXMLDocument {
	protected $body, $content;

	/*function CHTMLDocument ($html = NULL, $charset = NULL) {
        $this->CXMLDocument($html, $charset);
	}*/

    function load ($html, $charset = NULL) {
        if ($charset) $html = <<<HTML
<meta http-equiv="Content-Type" content="text/html; charset=$charset"/>$html
HTML;
        $this->DOM->loadHTML(strtr($html, "\r", ' '));
        $this->afterLoad();
    }

    function loadFrom ($uri, $charset = NULL) {
        $this->load(file_get_contents($uri, TRUE), $charset);
    }

    protected function afterLoad () {
        $this->DOM->normalize();
        $this->XPath = new DOMXPath($this->DOM);
        $this->body = $this->XPath->query('//body')->item(0);
        $this->content = $this->XPath->query('//body/*');
    }

    function DOM    () { return $this->DOM; }
    function XPath  () { return $this->XPath; }
    function node   () { return $this->body; }

    function traverse ($callback) {
        $this->traverse_($callback, $this->DOM->documentElement);
    }

    protected function traverse_ ($callback, $mixed, $level=0, $index=0) {
        if ($mixed instanceof DOMNodeList) {
            for ($i = 0; $i < $mixed->length; $i++) {
               	$this->traverse_($callback, $mixed->item($i), $level, $i);
            }
        }
        else if ($mixed instanceof DOMNode) {
            call_user_func($callback, $mixed, $level, $index);
            $this->traverse_($callback, $mixed->childNodes, $level+1);
        }
    }

    function debug ($node, $level, $index) {
        while (0 < $level--) {
            echo "    ";
        }
        echo "$index: $node->nodeName\r\n";
    }
}

class CHTMLTemplate extends CHTMLDocument {

	/*function CHTMLTemplate ($tpl = NULL, $charset = NULL) {
        $this->CHTMLDocument($tpl, $charset);
	}
    function load ($tpl, $charset = 'UTF-8') {
        parent::load($tpl, $charset);
    }
    function loadFrom ($uri, $charset = 'UTF-8') {
        parent::loadFrom($uri, $charset);
    }*/

    function source ($root = NULL) {
        return ($root) ? parent::source($root) :
            $this->sourceList_($this->content);
    }
    
    function traverse ($callback, $node = NULL, $level = 0, $index = 0) {
        $this->traverse_($callback, $this->content);
    }
}

class CElement extends CMarkupElement {
    protected $tagname;

    function CElement ($base, $node) {
        $this->CMarkupElement($base, $node);
        $this->tagname = $this->nodeName;
    }

    static function create($base, $node) {
        return new CElement($base, $node);
    }

    protected static function parseName ($name) {
        return $name;
    }

    function set_innerHTML ($html) {
        $this->removeChildren();
        if (!$html) return;//???//
        $f = $this->DOM()->createDocumentFragment();
        $f->appendXML($html);
        $this->node()->appendChild($f);
        return $this->source($f);
    }

    function get_innerHTML () {
        return $this->source(); //???//
    }
}


?>